<?php
/**
* 日志工厂对象
* @author     Steven.liao
*/
class Logger
{
	protected $_dir = '../log/';
	protected $_type = 'stream';
	protected $_priority = 7;
	protected $_module = 'System';
	protected $_table = 'logSystem';
		
	public function __construct()
	{}
	
	/**
	 * 静态方法 返回一个日志对象
	 * @return object
	 */
	public static function factory($module=null,$type=null,$priority=null,$dir=null)
	{
		$self = new self();
		if(!is_null($module)) $self->setModule($module);
    	if(!is_null($priority)) $self->setPriority($priority);
    	if(!is_null($type)) $self->setType($type);
    	if(!is_null($dir)) $self->setDir($dir);
    	return $self->_factory();
    	
	}
	
	/**
	 * 返回一个日志对象
	 */
	public function _factory()
	{
		$writer = $this->getWriter();
    	$logger = new Zend_Log($writer);
    	$filter = new Zend_Log_Filter_Priority($this->_priority);
    	$logger->addFilter($filter);
    	return $logger;
	}
	/**
	 * 设定日志目录
	 * @param string $dir
	 * @return object
	 */
	public function setDir($dir)
	{
		$this->_dir = $dir;
		return $this;
	}
	
	/**
	 * 设定日志等级
	 * @param int $priority
	 * @return object
	 */
	public function setPriority($priority)
	{
		$this->_priority = intval($priority);
		return $this;
	}
	
	/**
	 * 设定输出日志类型
	 * @param string $type
	 * @return object
	 */
	public function setType($type)
	{
		$this->_type = $type;
		return $this;
	}
	
	/**
	 * 日志分类名
	 * @param string $module
	 * @return object
	 */
	public function setModule($module)
	{
		$this->_module = $module;
		return $this;
	}
	
	/**
	 * 取得 日志对象的  Zend_Log_Writer
	 */
	public function getWriter()
	{
		switch ($this->_type){
			case 'stream':
				$writer = new Zend_Log_Writer_Stream($this->getfile());
				break;
			case 'db':
				$writer = new Zend_Log_Writer_Db(DB::getWDB(), $this->getTable());
				break;
			case 'firebug':
				break;
			case 'mock':
				break;
		}
		return $writer;
	}
	
	/**
	 * 取得日志文件名
	 */
	public function getFile()
	{
		return $this->_dir.$this->_module.'.txt';
	}
	
	/**
	 * 取得表名
	 */
	public function getTable()
	{	
		return 'log'.$this->_module;
	}
	
	public static function info($message){
		$self = new self();
		$logger = $self->_factory();
		$logger->info($message);
	}
}